package com.example.administrator.daydayappmarket.recommend.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.recommend.activity.RecommendMainThirdActivity;
import com.example.administrator.daydayappmarket.recommend.moduls.Details;
import com.example.administrator.daydayappmarket.recommend.view.HorizontalListView;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016/12/29.
 */

public class RecommendDetailsAdapter extends BaseAdapter {
    private Details details;
    private Context context;
    private String strings[] = {"展开", "收起"};
    private int i[] = {R.drawable.ic_arrow_down, R.drawable.ic_arrow_up};
    private int index = 0;

    public RecommendDetailsAdapter(Details details, Context context) {
        this.details = details;
        this.context = context;
    }

    @Override
    public int getCount() {
        return details != null ? 4 : 0;
    }

    @Override
    public Object getItem(int position) {
        return 1;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DecimalFormat df = new DecimalFormat("#.##");
        ViewHolder0 viewHolder0 = null;
        ViewHolder1 viewHolder1 = null;
        ViewHolder2 viewHolder2 = null;
        ViewHolder3 viewHolder3 = null;
        switch (position) {
            case 0:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.layout_recommend_main_third_0, parent, false);
                    viewHolder0 = new ViewHolder0(convertView);
                    convertView.setTag(viewHolder0);
                } else {
                    viewHolder0 = (ViewHolder0) convertView.getTag();
                }
                String icon_url = details.getData().getIcon_url();
                String name = details.getData().getName();
                String reviews = details.getData().getReviews();
                String category = details.getData().getCategory();
                String download_count = details.getData().getDownload_count();
                String package_size = details.getData().getPackage_size();
                double size = Double.parseDouble(package_size);
                double v = Double.parseDouble(download_count);
                String format = df.format(size);
                String format1 = df.format(v);

                if (icon_url.startsWith("http")) {
                    Glide.with(context).load(icon_url).into(viewHolder0.image);
                } else {
                    Glide.with(context).load(Contacts.URL_BASEIMAGE + icon_url).into(viewHolder0.image);
                }
                viewHolder0.name.setText(name);
                viewHolder0.count.setText(format + "MB  " + format1 + "万人下载");
                viewHolder0.comment.setText("小编点评：" + reviews);
                viewHolder0.text.setText(category);

                break;

            case 1:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.layout_recommend_main_third_1, parent, false);
                    viewHolder1 = new ViewHolder1(convertView);
                    convertView.setTag(viewHolder1);
                } else {
                    viewHolder1 = (ViewHolder1) convertView.getTag();
                }


                List<String> screenshoot_urls = details.getData().getScreenshoot_urls();
                for (int i1 = 0; i1 < screenshoot_urls.size(); i1++) {
                    View view1 = LayoutInflater.from(context).inflate(R.layout.item_recommend_main_third_1,null);
                    ImageView viewByIdimage = (ImageView) view1.findViewById(R.id.item_recommend_main_third_1_image);
                    if(screenshoot_urls.get(i1).startsWith("http")){
                            Glide.with(context).load(screenshoot_urls.get(i1)).into(viewByIdimage);
                    }else {
                        Log.e("tag", "getView: "+screenshoot_urls.get(i1) );
                        Glide.with(context).load(Contacts.URL_BASEIMAGE+screenshoot_urls.get(i1)).into(viewByIdimage);
                    }
                    viewHolder1.listview.addView(view1);
                }
                break;

            case 2:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.layout_recommend_main_third_2, parent, false);
                    viewHolder2 = new ViewHolder2(convertView);
                    convertView.setTag(viewHolder2);
                } else {
                    viewHolder2 = (ViewHolder2) convertView.getTag();
                }

                String created_at = details.getData().getCreated_at();
                String version_name = details.getData().getVersion_name();
                String description = details.getData().getDescription();
                Spanned spanned = Html.fromHtml(description);
                viewHolder2.xiangxi.setText(spanned);
                String s = created_at.replaceAll("-", "/");
                String substring = created_at.substring(0, s.lastIndexOf(" "));
                viewHolder2.gengxin.setText(substring);
                viewHolder2.banben.setText(version_name);
                final ViewHolder2 finalViewHolder = viewHolder2;
                viewHolder2.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        index++;
                        int into = index % 2;
                        if (into == 0) {
                            finalViewHolder.xiangxi.setMaxLines(2);
                        } else {
                            finalViewHolder.xiangxi.setMaxLines(100);
                        }

                        finalViewHolder.button.setText(strings[into]);
                        Drawable drawable = ContextCompat.getDrawable(context, i[into]);
                        drawable.setBounds(1, 1, 35, 35);
                        finalViewHolder.button.setCompoundDrawables(null, null, drawable, null);

                    }
                });
                viewHolder2.xiangxi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        index++;
                        int into = index % 2;
                        if (into == 0) {
                            finalViewHolder.xiangxi.setMaxLines(2);
                        } else {
                            finalViewHolder.xiangxi.setMaxLines(100);
                        }
                        finalViewHolder.button.setText(strings[into]);
                        Drawable drawable = ContextCompat.getDrawable(context, i[into]);
                        drawable.setBounds(1, 1, 35, 35);
                        finalViewHolder.button.setCompoundDrawables(null, null, drawable, null);
                    }
                });
                break;

            case 3:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.layout_recommend_main_third_3, parent, false);
                    viewHolder3 = new ViewHolder3(convertView);
                    convertView.setTag(viewHolder3);
                } else {
                    viewHolder3 = (ViewHolder3) convertView.getTag();
                }
                List<Details.DataBean.RecommendAppsBean> recommend_apps = details.getData().getRecommend_apps();
                for (int i1 = 0; i1 < recommend_apps.size(); i1++) {
                    View view3  =LayoutInflater.from(context).inflate(R.layout.item_recommend_main_third_3,null);
                    ImageView image3 = (ImageView) view3.findViewById(R.id.item_recommend_main_third_3_imageview);
                    TextView name3 = (TextView) view3.findViewById(R.id.item_recommend_main_third_3_name);
                    TextView size3 = (TextView) view3.findViewById(R.id.item_recommend_main_third_3_size);
                    LinearLayout search = (LinearLayout) view3.findViewById(R.id.item_recommend_main_third_3_search);

                    final Details.DataBean.RecommendAppsBean item = recommend_apps.get(i1);
                    String namei = item.getName();
                    final String id = item.getId();
                    String icon_urli = item.getIcon_url();
                    String download_url = item.getDownload_url();
                    String package_sizei = item.getPackage_size();
                    double vi = Double.parseDouble(package_sizei);
                    String formati = df.format(vi/1024/1024);
                    name3.setText(namei);
                    size3.setText(formati+"MB");
                    if(icon_urli.startsWith("http")){
                        Glide.with(context).load(icon_urli).into(image3);
                    }else {
                        Glide.with(context).load(Contacts.URL_BASEIMAGE+icon_urli).into(image3);
                    }
                    viewHolder3.listview.addView(view3);
                    search.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                            Log.e("TAG", "onClick: 点击了子控件"+id);
                            Intent intent = new Intent(context, RecommendMainThirdActivity.class);
                            intent.putExtra("id",id);
                            intent.putExtra("type",item.getApps_type());
//                          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(intent);
                        }
                    });


                }

                break;
        }

        return convertView;
    }


    static class ViewHolder0 {
        @InjectView(R.id.item_recommend_main_third_0_image)
        ImageView image;
        @InjectView(R.id.item_recommend_main_third_0_name)
        TextView name;
        @InjectView(R.id.item_recommend_main_third_0_count)
        TextView count;
        @InjectView(R.id.item_recommend_main_third_0_text)
        TextView text;
        @InjectView(R.id.layout_recommend_main_third_0_comment)
        TextView comment;

        ViewHolder0(View view) {
            ButterKnife.inject(this, view);
        }
    }


    static class ViewHolder2 {
        @InjectView(R.id.layout_recommend_main_third_2_button)
        Button button;
        @InjectView(R.id.layout_recommend_main_third_2_banben)
        TextView banben;
        @InjectView(R.id.layout_recommend_main_third_2_gengxin)
        TextView gengxin;
        @InjectView(R.id.layout_recommend_main_third_2_xiangxi)
        TextView xiangxi;

        ViewHolder2(View view) {
            ButterKnife.inject(this, view);
        }
    }

    static class ViewHolder3 {
        @InjectView(R.id.layout_recommend_main_third_3_listview)
        LinearLayout listview;

        ViewHolder3(View view) {
            ButterKnife.inject(this, view);
        }
    }

    static class ViewHolder1 {
        @InjectView(R.id.layout_recommend_main_third_1_listview)
        LinearLayout listview;

        ViewHolder1(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
